package com.jorigin.dcball.util;

import com.jorigin.dcball.po.DoubleBall;
import com.jorigin.dcball.po.TotalBall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author lzhq
 */
public class DcballFileUtils {

	private static final Logger logger = LoggerFactory.getLogger(DcballFactoryUtils.class);

	public static ArrayList<TotalBall> tblist = new ArrayList<TotalBall>();
	/**已开*/
	public static ArrayList<DoubleBall> dbopenlist = new ArrayList<DoubleBall>();

	public static String filename = "ssq_DESC.TXT";

	/**
	 *
	 */
	static{
		System.out.println("----------------开始加载文件--------------");
		long start = System.currentTimeMillis();
		DcballFileUtils dfu = new DcballFileUtils();
		readFileFromTxt(dfu.getFile(DcballFileUtils.filename));
		long end=System.currentTimeMillis();
		System.out.println("读取文件耗时【"+(end-start)+"】毫秒");
		System.out.println("----------------文件加载结束--------------");

		/*
		maxRed1(dbopenlist);
		minRed6(dbopenlist);
		get321Count(dbopenlist);
        get123Count(dbopenlist);
        get4Sub1(dbopenlist);
        */

	}

	/**
	 * getResource()方法会去classpath下找这个文件，获取到url resource,
	 		* 得到这个资源后，调用url.getFile获取到 文件 的绝对路径
	 * url.getFile() 得到这个文件的绝对路径
	 * 获取classpath下的文件
	 * @param fileName
	 */
	private File getFile(String fileName) {
		ClassLoader classLoader = getClass().getClassLoader();
		URL url = classLoader.getResource(fileName);
		System.out.println(url.getFile());
		File file = new File(url.getFile());
		System.out.println(file.exists());
		return file;
	}


	public static Date parseDate(String dateStr){
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return com.jorigin.common.Date.parseDate(dateStr,sdf);
	}

	/**
	 * 读行转对象
	 * @param lineTxt
	 * @return
	 */
  public static TotalBall stringToObject(String lineTxt){
	  TotalBall tb = new TotalBall();
	  String[] s1 = lineTxt.split("@");
	  tb.setOpendate(parseDate(s1[0]));
	  String[] s2 = s1[1].split("\\|");
	  tb.setBlue(Integer.parseInt(s2[1]));
	  String[] s3 = s2[0].split(",");
	  tb.setRed1(Integer.parseInt(s3[0]));
	  tb.setRed2(Integer.parseInt(s3[1]));
	  tb.setRed3(Integer.parseInt(s3[2]));
	  tb.setRed4(Integer.parseInt(s3[3]));
	  tb.setRed5(Integer.parseInt(s3[4]));
	  tb.setRed6(Integer.parseInt(s3[5]));
	  return tb;
  }

    /**
     * 解析字符串到对象
     * @param lineTxt
     * @return
     */
  public static DoubleBall strToDoubleBall(String lineTxt){
      DoubleBall db = new DoubleBall();
      String[] bs = lineTxt.split(" ");
      db.setRed1(Integer.parseInt(bs[2]));
      db.setRed2(Integer.parseInt(bs[3]));
      db.setRed3(Integer.parseInt(bs[4]));
      db.setRed4(Integer.parseInt(bs[5]));
      db.setRed5(Integer.parseInt(bs[6]));
      db.setRed6(Integer.parseInt(bs[7]));

  	return db;
  }
  /**
   * 解析TXT
   * @param filePath
   */
  public static void readTxtFile(String filePath){
        try {
                String encoding="UTF-8";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                    	if(lineTxt.startsWith("20")){
							tblist.add(stringToObject(lineTxt));
						}
                    }
                    read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
     
    }
	/**
	 * 解析TXT
	 * @param file
	 */
	public static void readFileFromTxt(File file){
		int i = 0;
		try {
			String encoding="UTF-8";
			if(file.isFile() && file.exists()){
				InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while((lineTxt = bufferedReader.readLine()) != null){
                    dbopenlist.add(strToDoubleBall(lineTxt));
                    i++;
				}
				read.close();
				System.out.println("文件读取了【"+i+"】行。");
			}else{
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}

	}

	public static void maxRed1(ArrayList<DoubleBall> dblist){
		int maxr1 = 0 ;
		for (DoubleBall db:dblist) {
			maxr1 = db.getRed1()>maxr1?db.getRed1():maxr1;
		}
		System.out.println("------------------------------最大红1【"+maxr1+"】");
	}

	public static void minRed6(ArrayList<DoubleBall> dblist){
		int maxr6 = 33 ;
		for (DoubleBall db:dblist) {
			maxr6 = db.getRed6()<maxr6?db.getRed6():maxr6;
		}
		System.out.println("------------------------------最小红6【"+maxr6+"】");
	}
	public static void get321Count(ArrayList<DoubleBall> dblist){
		int count = 0 ;
		for (DoubleBall db:dblist) {
			if(db.getRed3()<=11&&db.getRed5()<=22){
				count++;
				logger.info(db.toString());
			}
		}
		System.out.println("------------------------------321【"+count+"】");
	}
	public static void get123Count(ArrayList<DoubleBall> dblist){
		int count = 0 ;
		for (DoubleBall db:dblist) {
			if(db.getRed2()>11&&db.getRed3()<=22){
				count++;
				logger.info(db.toString());
			}
		}
		System.out.println("------------------------------123【"+count+"】");
	}
    public static void get4Sub1(ArrayList<DoubleBall> dblist){
	    int max41 = 0;
	    int min41 = 33;
        for (DoubleBall db:dblist) {
                int sub41 = db.getRed4()-db.getRed1();
                max41 = sub41>max41?sub41:max41;
                min41 = sub41<min41?sub41:min41;
                logger.info("4-1:"+sub41);
                logger.info("max4-1:"+max41);
                logger.info("min4-1:"+min41);
        }
    }
	public static void getless3(ArrayList<DoubleBall> dblist){

	}
	public static void main(String[] args){
		System.out.println("------------分析结束------------");
	}
}
